﻿using Dapr.Client;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;

namespace FrontEnd.Controllers;

[Route("api/[controller]")]
[ApiController]
public class DaprSecretManagementController : ControllerBase
{
    private readonly ILogger<DaprSecretManagementController> _logger;
    private readonly DaprClient _daprClient;
    private readonly IConfiguration _configuration;
    public DaprSecretManagementController(ILogger<DaprSecretManagementController> logger, DaprClient daprClient, IConfiguration configuration)
    {
        _logger = logger;
        _daprClient = daprClient;
        _configuration = configuration;
    }

    [HttpGet("GetSecret")]
    public async Task<ActionResult> GetSecretAsync()
    {
        _logger.LogInformation($"[{DateTime.Now}] ===> 进入 DaprSecretManagement.GetSecretsAsync");
        string storeName = _configuration["Dapr:Secrets:Development:FileName"];
        string key = _configuration["Dapr:Secrets:Development:Items:RabbitMQ"];
        Dictionary<string, string> secrets = await _daprClient.GetSecretAsync(storeName, key);
        string jsonSecrets = JsonSerializer.Serialize(secrets);
        _logger.LogInformation(jsonSecrets);
        return Ok(secrets[key]);
    }

    [HttpGet("GetBulkSecret")]
    public async Task<ActionResult> GetBulkSecretAsync()
    {
        _logger.LogInformation($"[{DateTime.Now}] ===> 进入 DaprSecretManagement.GetBulkSecretAsync");
        string storeName = _configuration["Dapr:Secrets:Development:FileName"];
        var secrets = await _daprClient.GetBulkSecretAsync(storeName);
        string jsonSecrets = JsonSerializer.Serialize(secrets);
        _logger.LogInformation(jsonSecrets);
        return Ok(secrets);
    }

}
